package com.example.leetcode;

/**
 * @description:
 * @author: zsw
 * @time: 2021/11/11 9:49
 */
public class AddTwoNumbers {

    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        if (l1==null && l2==null) {
            return null;
        }
        ListNode dummy= new ListNode(0);
        ListNode head = dummy;
        int addOne=0;
        while (l1!=null||l2!=null||addOne!=0)
        {
            int val1=l1==null?0:l1.val;
            int val2=l2==null?0:l2.val;
            int sum = val1+val2+addOne;
            head.next= new ListNode(sum%10);
            head= head.next;
            addOne=sum/10;
            if (l1!=null) {
                l1= l1.next;
            }
            if (l2!=null){
                l2=l2.next;
            }
        }
        return dummy.next;
    }


    public static class ListNode{
        int val;
        ListNode next;
       public ListNode(int val){
           this.val=val;
       }
       public static String print(ListNode l){
           StringBuilder sb = new StringBuilder();
           while (l!=null){
               sb.append(l.val);
               l=l.next;
           }
           System.out.println(sb.toString());
           return sb.toString();
       }
    }

    public static void main(String[] args) {
        ListNode l1= new ListNode(2);
        ListNode l2= new ListNode(5);
        l1.next = new ListNode(4);
        l2.next = new ListNode(6);
        System.out.println(ListNode.print(addTwoNumbers(l1,l2)));
    }
}
